/*
    This file is part of Titan.

    Titan is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as 
    published by the Free Software Foundation, either version 3 of 
    the License, or (at your option) any later version.

    Titan is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Titan. If not, see <http://www.gnu.org/licenses/>.
*/

/*
 * OpenFileFrame.java
 *
 * Created on 5. August 2008, 10:10
 */
package titan.mobile;

import java.io.File;

/**
 *
 * @author  Mirco Rossi <mrossi@ife.ee.ethz.ch>
 */
public class OpenFileFrame extends java.awt.Frame {

    File m_file;
    boolean m_fileIsSelected;
    TitanPanel m_panel;
    String m_selection;
    String m_currDirName;
    String m_currFileName;
    private final static String UP_DIRECTORY = "..";
    private final static String MEGA_ROOT = "\\";
    private final static String SEP_STR = "\\";

    /** Creates new form OpenFileFrame */
    public OpenFileFrame(TitanPanel p) {
        initComponents();
        m_currDirName = TitanMobile.CFG_PATH;   
        m_panel = p;
        showCurrDir();
    }

    void showCurrDir() {
        LoadButton.setEnabled(false);
        m_fileIsSelected = false;
        fileList.removeAll();
        String[] fileNames;
        m_file = new File(m_currDirName);

        fileNames = m_file.list();
        for (int i = 0; i < fileNames.length; i++) {
            fileList.add(fileNames[i]);
        }
        if (m_currDirName.equals(MEGA_ROOT)) {
        } else {
            fileList.add(UP_DIRECTORY, 0);
        }
        PathTextField.setText(m_currDirName);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        fileList = new java.awt.List();
        LoadButton = new java.awt.Button();
        PathTextField = new java.awt.TextField();

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        fileList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                fileListMouseClicked(evt);
            }
        });
        fileList.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fileListActionPerformed(evt);
            }
        });

        LoadButton.setLabel("load");
        LoadButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                LoadButtonActionPerformed(evt);
            }
        });

        PathTextField.setEditable(false);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, LoadButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, fileList, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE)
                    .add(PathTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(PathTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(fileList, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 259, Short.MAX_VALUE)
                .add(15, 15, 15)
                .add(LoadButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
        System.exit(0);
    }//GEN-LAST:event_exitForm
    public File getFile() {
        if (m_fileIsSelected) {
            return m_file;
        } else {
            return null;
        }
    }
private void fileListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_fileListMouseClicked
    m_selection = fileList.getSelectedItem();

    if (m_selection.equals(UP_DIRECTORY)) {
        int i = m_currDirName.lastIndexOf(SEP_STR, m_currDirName.length() - 2);
        if (i != -1) {
            m_currDirName = m_currDirName.substring(0, i + 1);
        } else {
            m_currDirName = MEGA_ROOT;
        }

        showCurrDir();
    } else {
        m_currFileName = m_currDirName + m_selection;
        m_file = new File(m_currFileName);
        if (m_file.isDirectory()) {
            m_currDirName = m_currFileName + SEP_STR;
            showCurrDir();
        } else {
            LoadButton.setEnabled(true);
            m_fileIsSelected = true;
        }
    }
}//GEN-LAST:event_fileListMouseClicked

private void fileListActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fileListActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_fileListActionPerformed

private void LoadButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_LoadButtonActionPerformed

    m_panel.sendCommand("titan load \"" + m_file.getAbsolutePath() + "\"");
    this.dispose();
}//GEN-LAST:event_LoadButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.awt.Button LoadButton;
    private java.awt.TextField PathTextField;
    private java.awt.List fileList;
    // End of variables declaration//GEN-END:variables
}
